package cn.gxa.j229.bug.finance.controller.loanuser;

import cn.gxa.j229.bug.finance.BusinessException;
import cn.gxa.j229.bug.finance.pojo.LoanUser;
import cn.gxa.j229.bug.finance.service.loanuser.LoanUserService;
import cn.gxa.j229.bug.finance.utils.CreateNumber;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.MD5Util;
import cn.gxa.j229.bug.finance.utils.Salt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: fenglin
 * @Date:2020/9/22 16:35
 * @Version 1.0
 */
@Controller
@RequestMapping("/loanUser")
public class LoanUserController {

    @Resource
    LoanUserService loanUserService;

    
    JsonResult jsonResult = new JsonResult();

    @PostMapping("/select")
    @ResponseBody
    public Map findAll(@RequestBody LoanUser loanUser){

        Integer limit = loanUser.getLimit();
        Integer page = loanUser.getPage();
        List<Map<String, String>> select = loanUserService.selectByPrimaryKey(1, 5);
        int count = loanUserService.selectByPrimaryKeyCount(1, 5);
        jsonResult.put("count",count);


        jsonResult.setData(select);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }
    @PostMapping("/selectSock")
    @ResponseBody
    public Map selectSock(@RequestBody LoanUser loanUser){


        List<Map<String, String>> select = loanUserService.selectSock(loanUser);
        int count = loanUserService.selectSockCount(loanUser);
        jsonResult.put("count",count);


        jsonResult.setData(select);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }
    @PostMapping("/selectLikeName")
    @ResponseBody
    public Map selectLikeName(@RequestBody LoanUser loanUser){
        String username = loanUser.getUsername();
        Integer limit = loanUser.getLimit();
        Integer page = loanUser.getPage();
        List<Map<String, String>> select = loanUserService.selectLikeName(loanUser);
        int count = loanUserService.selectLikeNameCount(loanUser);

        jsonResult.put("count",count);
        jsonResult.setData(select);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }


    @PostMapping("/selectPhoneAndNameAndSock")
    @ResponseBody
    public Map findPhone(@RequestBody LoanUser loanUser){
        Integer limit = loanUser.getLimit();
        Integer page = loanUser.getPage();

        String phone = loanUser.getPhone();
        String username = loanUser.getUsername();
        Integer sock = loanUser.getSock();

        List<Map<String, String>> selectUsername = loanUserService.selectUsername(loanUser);
        int count = loanUserService.selectUsernameCount(loanUser);
        jsonResult.put("count",count);
            jsonResult.setData(selectUsername);



        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }

    @PostMapping("/update")
    @ResponseBody
    public Map update(@RequestBody LoanUser loanUser){
        int update = loanUserService.update(loanUser);


        jsonResult.setData(update);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();
    }
    @PostMapping("/save")
    @ResponseBody
    public Map save(@RequestBody LoanUser loanUser){
        System.out.println(loanUser.getPassword());
        System.out.println(loanUser.getRpassword());
        String password = loanUser.getPassword();
        if(password.equals(loanUser.getRpassword())){
            String salt = Salt.createSalt();
            loanUser.setSalt(salt);

            String encode = MD5Util.encode(password + salt);
            loanUser.setPassword(encode);
            loanUser.setRegistration(new Date());
            loanUser.setLoginime(new Date());
            loanUser.setUsernumber(CreateNumber.getNum());
            int save = loanUserService.insert(loanUser);


                jsonResult.setData(save);
                jsonResult.setCode("200");
                jsonResult.setMsg("成功");
            }else{


                jsonResult.setMsg ("您输入的密码和确认密码不相同");
            }
        return jsonResult.getValues();
    }
}
